package com.yoomiito.app.ui.my.remain.transfer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.model.my.PayeeInfo;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.CheckUtil;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SoftKeyBoardUtils;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.EditTextAndDel;
import com.yoomiito.app.widget.dialog.TipDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class InputTelFragment extends BaseLazyFragment<InputTelPresenter> {

    @BindView(R.id.telInput)
    EditTextAndDel mEditTextAndDel;
    @BindView(R.id.parent_view)
    LinearLayout ll;
    @BindView(R.id.sure)
    TextView mNextTv;

    private TipDialog tipDialog;
    private PayeeInfo mPayeeInfo;

    @Override
    public int getLayoutId() {
        return R.layout.fm_transfer_input_tel;
    }

    @Override
    public InputTelPresenter newP() {
        return new InputTelPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        SoftKeyBoardUtils.showView(ll, mNextTv);

        mEditTextAndDel.setCursorColor(R.drawable.cursor);
        mEditTextAndDel.setTypeface(AllUtils.getTypeface());
        mEditTextAndDel.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        mEditTextAndDel.setMaxLength(11);
        mEditTextAndDel.setIvWidth(DensityUtil.dp2px(16), DensityUtil.dp2px(16));
    }

    @OnClick({R.id.sure})
    public void onClick(View view) {
        if (view.getId() == R.id.sure) {
//
            String mobile = mEditTextAndDel.getEditText().getText().toString().trim();
            if (TextUtils.isEmpty(mobile)) {
                YMToast.showText("请输入收款人手机号码");
                return;
            }
            if (!CheckUtil.isMobileNO(mobile)) {
                YMToast.showText(getString(R.string.error_phone_toast));
                return;
            }
            if (mobile.equals(UserInfoConfig.getTel())) {
                YMToast.showText("不能向自己账号转账");
                return;
            }
            getP().getInfo(mobile);
        }
    }

    public void payeeInfo(PayeeInfo payeeInfo) {
        mPayeeInfo = payeeInfo;
        if (payeeInfo.getCompany_id()!=UserInfoConfig.getCompanyId()) {
            showTip("账号错误","只能转账给团队其他成员","确认");
            return;
        }
        TransferActivity transferActivity = ((TransferActivity) context);
        transferActivity.setTransferData(payeeInfo);
        transferActivity.switchFm(1);
    }

    public void showTip(String title,String content,String sureContent) {
        if (tipDialog == null) {
            tipDialog = new TipDialog(context);
        }

        tipDialog.setTitle(title)
                .setContent(content)
                .setCancelText(sureContent)
                .setCancelTextColor(ResUtil.getColor(R.color.color_orange))
                .show();
    }

    @Override
    protected void onDestroyLazy() {
//        SoftKeyBoardUtils.removeListener();
        super.onDestroyLazy();
    }
}
